package view;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import model.Author;
import model.Conference;

import controller.DataBaseInteactor;

/**
 * Makes a Frame helps submitting manuscript.
 * 
 * @author Richard Lee
 * @version 6513
 */
public class ManuscriptSumitView extends JFrame {

	/**
	 * Default serial version id.
	 */
	private static final long serialVersionUID = -2824461225168721425L;
	
	/**
	 * GUI component.
	 */
	private final JPanel bottomPanel = new JPanel(new FlowLayout());
	private final JPanel titlePanel = new JPanel(new FlowLayout());
	private final JPanel authorPanel = new JPanel(new FlowLayout());
	private final JPanel bodyPanel = new JPanel(new FlowLayout());
	private final JLabel titleLabel = new JLabel("Title");
	private final JLabel authorLabel = new JLabel("Author");
	private final JLabel bodyLabel = new JLabel("Body");
	private final JTextField titleField = new JTextField(30);
	private final JTextField authorField = new JTextField(30);
	private final JTextArea bodyField = new JTextArea(10, 30);
	private final JButton submitButton = new JButton("Sumbit");
	private final JButton cancelButton = new JButton("Cancel");
	
	/**
	 * User id.
	 */
	private final String id;
	
	/**
	 * Conference that user belongs to.
	 */
	private final Conference conf;
	
	/**
	 * Database interactor.
	 */
	private final DataBaseInteactor inteactor;
	
	/**
	 * Constructor. 
	 * 
	 * @param theInteactor database interactor.
	 * @param theId user id.
	 * @param theConf conference that user belongs to.
	 */
	public ManuscriptSumitView(DataBaseInteactor theInteactor, String theId, Conference theConf) {
		super("Sumit Manscript");
		inteactor = theInteactor;
		id = theId;
		conf = theConf;
	}
	
	/**
	 * Combine and add all the GUI component.
	 */
	public void createManuscriptSumitView() {
		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
		titlePanel.add(titleLabel);
		titlePanel.add(titleField);
		authorPanel.add(authorLabel);
		authorPanel.add(authorField);
		bodyPanel.add(bodyLabel);
		bodyPanel.add(bodyField);
		bodyField.setBorder(BorderFactory.createLineBorder(Color.black));
		bottomPanel.add(submitButton);
		bottomPanel.add(cancelButton);
		p.add(titlePanel);
		p.add(authorPanel);
		p.add(bodyPanel);
		p.add(bottomPanel);
		add(p);
		setVisible(true);
		pack();
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		actionButton();
	}
	
	/**
	 * Add actions to button.
	 */
	private void actionButton() {
		submitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Author author = new Author(id, inteactor);
				author.submit(id, conf, titleField.getText(), authorField.getText(), 
						bodyField.getText());
			}
		});
		
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
	}
}
